﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using BOHVote.BLL;
using System.Text;

namespace BOH.DataManagement
{
    public partial class UserAccessList : System.Web.UI.Page
    {
        private string _DivHtmlRight = string.Empty;
        public string DivHTMLRights
        {
            get { return getRights(); }
            set { _DivHtmlRight = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                AccessBind();
            }
                
        }

        public string getRights()
        {
            StringBuilder sb = new StringBuilder();
            DataSet dst = UserClass.dstGetRight();
            sb.Append(@"<div id=\'showRightList\' style=\'padding:0px;height:430px;width:100%;border: #99bbe8 1px solid;OVERFLOW-y:auto\'>");
            sb.Append(@"<ul style=\'list-style-type:none;text-align:left;\'>");
            sb.Append(@"<li style=\'height:28px;color:black;\'>");
            sb.Append(@" <input type=\'Hidden\' id=\'rightID_0\' value=\'\'/><a href=# onclick=SelectRight(\'rightID_0\',\''+obj+'\',\'无权限选择\')>无权限选择</a> ");
            sb.Append("</li>");
            foreach (DataRow dr in dst.Tables[0].Rows)
            {
                sb.Append(@"<li style=\'height:28px;color:black;\'>");
                sb.Append(@" <input type=\'Hidden\' id=\'rightID_" + dr["RightID"].ToString() + @"\' value=\'" + dr["RightID"].ToString() + @"\'/><a href=# onclick=SelectRight(\'rightID_" + dr["RightID"].ToString() + @"\',\''+obj+'\',\'" + dr["RightName"].ToString() + @"\')>" + dr["RightName"].ToString() + "</a> ");
                sb.Append("</li>");
            }
            sb.Append("</ul>");
            sb.Append("</div>");

            //DivHTMLRights = sb.ToString();\
            //divPopup.Text = sb.ToString();
            return sb.ToString();
        }

        public void AccessBind()
        {
            DataSet dst = null;
            dst = UserClass.getAllAccess(this.txtUserName.Text.Trim(), this.RightSelect_hiddenRight.Value.Trim());

            BindAccessList(dst);
        }

        public void BindAccessList(DataSet dst)
        {
            //UserViewList.DataSource = dst;
            //UserViewList.DataBind();
            AspNetPager1.RecordCount = dst.Tables[0].Rows.Count;
            if (AspNetPager1.RecordCount != 0)
            {
                labNoRecord.Visible = false;
                AspNetPager1.Visible = true;
                PagedDataSource pds = new PagedDataSource();
                pds.DataSource = dst.Tables[0].DefaultView;
                pds.AllowPaging = true;
                pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
                pds.PageSize = AspNetPager1.PageSize;
                AccessViewList.DataSource = pds;
                AccessViewList.DataBind();
            }
            else
            {
                AccessViewList.DataSource = null;
                AccessViewList.DataBind();
                AspNetPager1.Visible = false;
                labNoRecord.Visible = true;
            }
        }

        public string getStatus(string status)
        {
            string returnvalue = string.Empty;
            switch (status)
            {
                case "A":
                    returnvalue = "激活";
                    break;
                case "D":
                    returnvalue = "<font color='red'>未激活</font>";
                    break;
                default:
                    break;
            }

            return returnvalue;
        }

        public string getAccess(string Levels)
        {
            string returnvalue = string.Empty;
            switch (Levels.ToUpper().Trim())
            {
                case "A":
                    returnvalue = "系统管理员";
                    break;
                case "U":
                    returnvalue = "单位用户组";
                    break;
                case "C":
                    returnvalue = "中心用户组";
                    break;
                case "G":
                    returnvalue = "主管局用户组";
                    break;
                case "S":
                    returnvalue = "学科组管理员";
                    break;
                case "P":
                    returnvalue = "个人用户组";
                    break;
                case "Q":
                    returnvalue = "试题组管理员";
                    break;
                default:
                    break;
            }

            return returnvalue;
        }

        protected void butSearch_Click(object sender, EventArgs e)
        {
            AccessBind();
        }


        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            AccessBind();
        }

        protected void AccessViewList_RowEditing(object sender, GridViewEditEventArgs e)
        {
            
            AccessViewList.EditIndex = e.NewEditIndex;
            AccessBind();
        }

        protected void AccessViewList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if ((e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState != DataControlRowState.Normal && e.Row.RowState != DataControlRowState.Alternate))
            {
                ////HiddenField hid = (HiddenField)(e.Row.FindControl("hiddenRight"));
                //DropDownList drpAccess = (DropDownList)(e.Row.FindControl("drpAccessList"));
                // .SelectedValue = hid.Value;
            }
        }

        protected void AccessViewList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            AccessViewList.EditIndex = -1;
            AccessBind();
        }

        protected void AccessViewList_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string Accessid = ((HiddenField)AccessViewList.Rows[e.RowIndex].FindControl("hidSURID")).Value;
            string UserID = AccessViewList.DataKeys[e.RowIndex].Values[1].ToString();
            string RightID = ((HiddenField)AccessViewList.Rows[e.RowIndex].FindControl("txtAccess_hiddenRight")).Value;
            if (Accessid.Trim().Length > 0)
                UserClass.UpdateUserAccess(Accessid, RightID, UserID);
            else
            {
                UserClass.InsertUserAccess(UserID, RightID, "A");
            }
            AccessViewList.EditIndex = -1;
            AccessBind();
        }
    }
}
